package com.wu.llpsw.event;


import com.wu.llpsw.data.levelData;
import com.wu.llpsw.data.tianFu;
import com.wu.llpsw.item.ModItems;
import com.wu.llpsw.networking.ModMessage;
import com.wu.llpsw.networking.packet.C2SPacket;
import com.wu.llpsw.networking.packet.backYuanLiMaxPacket;
import com.wu.llpsw.networking.packet.backYuanLiNowPacket;
import com.wu.llpsw.tool.select;
import com.wu.llpsw.yuanli.energy.yuanliManager;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

/**
 * 实体加入世界的逻辑
 */

@Mod.EventBusSubscriber(modid = "wu")
public class joinWorld {

    @SubscribeEvent
    public static void init(PlayerEvent.PlayerLoggedInEvent event){
        Player player=event.getPlayer();
        //获取服务器实例
        //MinecraftServer minecraftServer = (MinecraftServer) LogicalSidedProvider.WORKQUEUE.get(LogicalSide.SERVER);
        //如果加入的玩家不为空
        if(player!=null){
            boolean firstJoin= select.isFirstJoin(player);//查询玩家是否是第一次加入游戏
            //新加入的玩家给与一个基础凝气术
            if(firstJoin){
                player.addItem(new ItemStack(ModItems.yuanZunJue.get(), 1));
            }
            tianFu.setLevel(player,firstJoin);
        }
    }



    @SubscribeEvent
    public static void createEntity(EntityJoinWorldEvent event){
        if(!event.getWorld().isClientSide()){
            //如果加入的是玩家
            if(event.getEntity() instanceof Player){
                Player player=(Player)event.getEntity();
                levelData.setParam(player,player.experienceLevel);
                ModMessage.sendToPlayer(new backYuanLiMaxPacket(yuanliManager.getMaxYuanLi(player)),(ServerPlayer) player);
                ModMessage.sendToPlayer(new backYuanLiNowPacket(yuanliManager.getNowYuanLi(player)),(ServerPlayer)player);
            }
        }
    }

}
